package bll

import (
	"context"
	"github.com/LyricTian/gin-admin/v7/internal/app/model/gormx/model"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/errors"
	"github.com/LyricTian/gin-admin/v7/pkg/util/uuid"
	"github.com/google/wire"
)

// GroupMemberSet 注入GroupMember
var GroupMemberSet = wire.NewSet(wire.Struct(new(GroupMember), "*"))

// GroupMember 群成员
type GroupMember struct {
	GroupMemberModel *model.GroupMember
	UserInfoModel *model.UserInfo
}

// Query 查询数据
func (a *GroupMember) Query(ctx context.Context, params schema.GroupMemberQueryParam, opts ...schema.GroupMemberQueryOptions) (*schema.GroupMemberQueryResult, error) {
	return a.GroupMemberModel.Query(ctx, params, opts...)
}

// GetFirst 查询数据
func (a *GroupMember) GetFirst(ctx context.Context, params schema.GroupMemberQueryParam, opts ...schema.GroupMemberQueryOptions) (*schema.GroupMember, error) {
	item, err := a.GroupMemberModel.GetFirst(ctx, params, opts...)
	if err != nil {
		return nil, err
	}
	if item == nil {
		return nil, errors.ErrNotFound
	}

	return item, nil
}

// Get 查询指定数据
func (a *GroupMember) Get(ctx context.Context, id string, opts ...schema.GroupMemberQueryOptions) (*schema.GroupMember, error) {
	item, err := a.GroupMemberModel.Get(ctx, id, opts...)
	if err != nil {
		return nil, err
	} else if item == nil {
		return nil, errors.ErrNotFound
	}

	return item, nil
}

// Create 创建数据
func (a *GroupMember) Create(ctx context.Context, item schema.GroupMember) (*schema.IDResult, error) {

	item.ID = uuid.NewID()
	err := a.GroupMemberModel.Create(ctx, item)
	if err != nil {
		return nil, err
	}

	return schema.NewIDResult(item.ID), nil
}

// Update 更新数据
func (a *GroupMember) Update(ctx context.Context, id string, item schema.GroupMember) error {
	_, err := a.Get(ctx, id)
	if err != nil {
		return err
	}

	return a.GroupMemberModel.Update(ctx, id, item)
}

// Delete 删除数据
func (a *GroupMember) Delete(ctx context.Context, id string) error {
	item, err := a.GroupMemberModel.Get(ctx, id)
	if err != nil {
		return err
	}
	if item == nil {
		return errors.ErrNotFound
	}

	return a.GroupMemberModel.Delete(ctx, id)
}

// UpdateStatus 更新状态
func (a *GroupMember) UpdateStatus(ctx context.Context, id string, status int) error {
	item, err := a.GroupMemberModel.Get(ctx, id)
	if err != nil {
		return err
	}
	if item == nil {
		return errors.ErrNotFound
	}

	return a.GroupMemberModel.UpdateStatus(ctx, id, status)
}
